//
//  QMQCComplicationController.m
//  QcMedical1
//
//  Created by 仲启明 on 2016/12/22.
//  Copyright © 2016年 jiawen Du. All rights reserved.
//

#import "QMQCComplicationController.h"
#import "QMQCComplicationViewCell.h"
#import "QMQCComplicationModel.h"
@interface QMQCComplicationController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, copy) NSString *effectName;
@property(nonatomic,strong) NSArray <QMQCComplicationModel *>*compModelList;
@end

@implementation QMQCComplicationController

- (NSMutableArray<NSString *> *)mArray
{
    if (!_mArray) {
        
        _mArray = [NSMutableArray array];
    }
    
    return _mArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getData];
    
    [self setupUI];
}
- (void)getData
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Complication.plist" ofType:nil];
    
    NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
    
    NSMutableArray *tempArray = [NSMutableArray array];
    
    for (NSDictionary *dic in array)
    {
        QMQCComplicationModel *model = [QMQCComplicationModel new];
        
        [model setValuesForKeysWithDictionary:dic];
        
        [tempArray addObject:model];
    }
    _compModelList = tempArray.copy;
    

}
- (void)setupUI
{
    UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    
    [addBtn setImage:[UIImage imageNamed:@"complite"] forState:UIControlStateNormal];
    
    [addBtn addTarget:self action:@selector(clickCompleteButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:addBtn];
    
    
    self.navigationItem.rightBarButtonItem =  rightItem;


    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    
    
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    
    titleLabel.text =  @"并发症";
    
    titleLabel.font = [UIFont fontWithName:@"CloudShuiZhuGBK" size:18];
    self.navigationItem.titleView = titleLabel;
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    [self.view addSubview:tableView];
    
    self.tableView = tableView;
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    [tableView registerNib:[UINib nibWithNibName:@"QMQCComplicationViewCell" bundle:nil] forCellReuseIdentifier:@"complicate"];
    
    //允许多选
    self.tableView.allowsMultipleSelection = YES;
}

- (void)clickCompleteButton
{

    if (_mArray.count == 0) {
       
        return;
    }
    if (self.DiseaseSubDivisionBlock)
    {
        
        NSMutableString *str = [NSMutableString new];
        [_mArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx == 0) {
                [str appendFormat:@"%@",obj];
            }
            if (idx == 1) {
                [str appendFormat:@",%@",obj];
            }
            if (idx >= 2 ) {
                [str appendFormat:@"..."];
                return;
            }
        }];
        self.DiseaseSubDivisionBlock(str.copy,_mArray);
    }
    
   
    [self.navigationController popViewControllerAnimated:YES];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.compModelList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QMQCComplicationViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"complicate" forIndexPath:indexPath];
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:68/255.0 green:175/255.0 blue:178/255.0 alpha:.3];
    
    QMQCComplicationModel *model = self.compModelList[indexPath.row];
    
    cell.model = model;
    
    
    if ([self.mArray containsObject:cell.model.complication_name]) {
        
        [cell.btn_ding setSelected:YES];
        
    }
    
    cell.lb_illName.text = model.complication_name;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    QMQCComplicationViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    
 
    cell.btn_ding.selected = cell.isSelected;
    
    if (cell.btn_ding.isSelected == YES)
    {
        [self.mArray addObject:self.compModelList[indexPath.row].complication_name];
        
    }
    else
    {
        if ([self.mArray containsObject:self.compModelList[indexPath.row].complication_name])
        {
            [self.mArray removeObject:self.compModelList[indexPath.row].complication_name];
        }
    }
    
}

@end
